﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Sidvall.AuthorizationManager.Model.DataPortalManagers
{
	public partial class AuthorizationScopeUserManager<TEntity, TList>
	{
		#region Public Members

		#region GetUsersAsync

		public async Task<TList> GetUsersAsync(int? serverConnectionId, IEnumerable<long> authorizationScopeIds, IEnumerable<string> userIds, int? rowCount, bool sortReversed)
		{
			var criteria = Sidvall.Data.CriteriaManager.NewFetchListCriteria<Data.Fields.AuthorizationScopeUserField, Data.Relations.AuthorizationScopeUserRelation>();
			if (rowCount != null)
			{
				criteria.RowCount = rowCount.Value;
				criteria.RowStartIndex = 0;
			}

			// Sort
			criteria.SortItems = new Sidvall.Data.SortItemCollection<Data.Fields.AuthorizationScopeUserField>();
			if (sortReversed)
			{
				criteria.SortItems.Add(Data.Fields.AuthorizationScopeUserField.UserId, false);
			}
			else
			{
				criteria.SortItems.Add(Data.Fields.AuthorizationScopeUserField.UserId);
			}

			// Filter
			criteria.Filter = new Sidvall.Data.Filter<Data.Fields.AuthorizationScopeUserField>();
			if ((authorizationScopeIds != null) && (authorizationScopeIds.Any()))
				criteria.Filter.AddFilterItem(Data.Fields.AuthorizationScopeUserField.AuthorizationScopeId, Sidvall.Data.ComparisonOperator.Equal, authorizationScopeIds);
			if ((userIds != null) && (userIds.Any()))
				criteria.Filter.AddFilterItem(Data.Fields.AuthorizationScopeUserField.UserId, Sidvall.Data.ComparisonOperator.Equal, userIds);

			// Parameters
			ParameterManager.InitializeParameters(criteria, serverConnectionId);

			return await GetItemsAsync(criteria).ConfigureAwait(false);
		}

		#endregion

		#endregion
	}
}